package app1.rapidsquare.com.activity;


import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;

import app1.rapidsquare.com.Utility.Utility;
import app1.rapidsquare.com.activity.R;
import app1.rapidsquare.com.classes.User;
import app1.rapidsquare.com.core.ConnectionDetector;
import app1.rapidsquare.com.core.ILoadComplete;
import app1.rapidsquare.com.database.AgileEventsProvider;
import app1.rapidsquare.com.database.AgileSyncService;

public class StartActivity extends Activity implements ILoadComplete {

    private ConnectionDetector mConnectionDetector;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        mImageView = (ImageView) findViewById(R.id.imageViewLogo);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int width = metrics.heightPixels;
        int height = metrics.widthPixels;

        mConnectionDetector = new ConnectionDetector(this);
        Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.logo_white, width * 2 / 3, height / 4);

        if (bitmap != null)
            mImageView.setImageBitmap(bitmap);
        if (IsAvailableInternet()) {

            Intent serviceIntent = new Intent(getApplicationContext(), AgileSyncService.class);
            startService(serviceIntent);

            Utility.AgileSyncUpdater(new AgileEventsProvider(getApplicationContext()), this);
        } else
            onLoadComplete(null);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Utility.HOW_IT_WORKS_ACTIVITY_REQUEST_CODE) {
            Intent intent = new Intent(StartActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected boolean IsAvailableInternet() {
        return mConnectionDetector.getConnectingStatus();
    }

    @Override
    public void onLoadComplete(User user) {
        User userSession = Utility.getUserSession(StartActivity.this);
        Intent intent;

        if (userSession == null || Utility.isNullOrEmpty(userSession.UserToken)) {
            intent = new Intent(StartActivity.this, HowItWorksActivity.class);
            startActivityForResult(intent, Utility.HOW_IT_WORKS_ACTIVITY_REQUEST_CODE);
        } else {
            intent = new Intent(StartActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}
